package com.dfbz.dao;

import com.dfbz.dao.Base.BaseDao;
import com.dfbz.model.Meeting;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class MeetingDao extends BaseDao<Meeting> {
    private JdbcTemplate jdbcTemplate=getJdbcTemplate();
    public MeetingDao() {
        super(Meeting.class);
    }
    public List<Meeting> findAllMeeting(String meeting_title,Integer status,Integer startIndex,Integer pageSize){
        String sql;
        List<Meeting> list;
        if (status==-1){
            sql="select * " +
                    "from meeting m " +
                    "where m.title like ? limit ?,?";
            list=jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(Meeting.class),"%"+meeting_title+"%",startIndex,pageSize);
        }
        else {
            sql="select * " +
                    "from meeting m " +
                    "where `status`=? and m.title like ? limit ?,?";
            list=jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(Meeting.class),status,"%"+meeting_title+"%",startIndex,pageSize);
        }
        return list;
    }

    public Integer coutByTilSta(String meeting_title, Integer status) {
        String sql;
        try {
            if (status==-1){
                sql="select count(1) " +
                        "from meeting m " +
                        "where m.title like ? ";
                return jdbcTemplate.queryForObject(sql,Integer.class,meeting_title);
            }
            else {
                sql="select count(1) " +
                        "from meeting m " +
                        "where `status`=0 and m.title like ? ";
                return jdbcTemplate.queryForObject(sql,Integer.class,status,meeting_title);
            }
        }catch (Exception e){
            return null;
        }
    }
}
